﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class Dropdown extends Sprite
    {
        var click:IconButton;
        var holder:Sprite;
        var content:Sprite;
        var sb:Scrollbar;
        var tb:TextInput;
        var cwidth:int;
        var active:Boolean = false;
        var item:SelectItem;
        var markedIndex:int = 0;
        public var length:int = 0;
        var disable:Sprite;

        public function Dropdown(param1:int, param2:int)
        {
            this.cwidth = param2;
            this.holder = new Sprite();
            this.content = new Sprite();
            this.click = new IconButton(null, "", new DropdownOver());
            this.click.addEventListener(MouseEvent.CLICK, this.Open, false, 0, true);
            this.click.x = param1 - this.click.width;
            addChild(this.click);
            this.tb = new TextInput(param1 - this.click.width);
            this.tb.addEventListener(MouseEvent.CLICK, this.Open, false, 0, true);
            this.tb.t.multiline = false;
            this.tb.t.selectable = false;
            this.tb.t.type = TextFieldType.DYNAMIC;
            addChild(this.tb);
            return;
        }// end function

        private function MarkIndex(flash.text:int) : SelectItem
        {
            if (this.item != null)
            {
                this.item.Select(false);
            }
            var _loc_2:* = flash.text < 0 ? (0) : (flash.text >= this.length ? ((this.length - 1)) : (flash.text));
            this.markedIndex = _loc_2;
            this.item = this.content.getChildAt(_loc_2) as SelectItem;
            this.item.Select();
            return this.item;
        }// end function

        public function Disable(markedIndex:Boolean = true) : void
        {
            if (this.disable == null)
            {
                this.disable = new Sprite();
                this.disable.visible = false;
                this.disable.alpha = 0.3;
                this.disable.graphics.beginFill(9474192);
                this.disable.graphics.drawRect(0, 0, 10, 10);
                this.disable.graphics.endFill();
                this.disable.height = this.tb.height;
                this.disable.width = this.tb.width + this.click.width;
                addChild(this.disable);
            }
            this.disable.visible = markedIndex;
            return;
        }// end function

        public function Set(markedIndex:String) : void
        {
            var _loc_2:int = 0;
            if (this.item != null)
            {
                this.item.Select(false);
            }
            while (_loc_2 < this.content.numChildren)
            {
                
                this.item = this.content.getChildAt(_loc_2) as SelectItem;
                if (this.item.name == markedIndex)
                {
                    break;
                }
                _loc_2++;
            }
            this.tb.text = this.item.text;
            this.item.Select();
            this.markedIndex = this.item.index;
            return;
        }// end function

        public function AddItem(markedIndex:String, markedIndex:String, markedIndex:Boolean = false) : void
        {
            var _loc_4:* = new SelectItem(markedIndex, markedIndex, this.cwidth);
            _loc_4.addEventListener(MouseEvent.CLICK, this.ItemClick, false, 1, true);
            _loc_4.addEventListener(MouseEvent.MOUSE_OVER, this.ItemOver, false, 1, true);
            _loc_4.y = Math.round(this.content.height);
            _loc_4.x = 1;
            this.content.addChild(_loc_4);
            if (markedIndex)
            {
                if (this.item != null)
                {
                    this.item.Select(false);
                }
                this.item = _loc_4;
                _loc_4.Select();
                this.tb.text = _loc_4.text;
                this.markedIndex = this.length;
            }
            _loc_4.index = this.length;
            var _loc_5:String = this;
            var _loc_6:* = this.length + 1;
            _loc_5.length = _loc_6;
            return;
        }// end function

        public function Clear() : void
        {
            while (this.content.numChildren > 0)
            {
                
                this.content.removeChildAt(0);
            }
            this.Bind();
            return;
        }// end function

        public function Bind() : void
        {
            this.DrawHolder();
            return;
        }// end function

        private function ItemClick(event:MouseEvent) : void
        {
            if (this.item != null)
            {
                this.item.Select(false);
            }
            this.item = event.currentTarget as SelectItem;
            this.item.Select();
            this.HideContent();
            this.tb.text = this.item.text;
            this.markedIndex = this.item.index;
            dispatchEvent(new SelectEvent(SelectEvent.COMMAND, this.item.name));
            return;
        }// end function

        private function ItemOver(event:Event) : void
        {
            dispatchEvent(new SelectEvent(SelectEvent.HOVER, (event.currentTarget as SelectItem).name));
            return;
        }// end function

        private function DrawHolder() : void
        {
            var _loc_1:* = (this.content.height + 1) > 180 ? (180) : ((this.content.height + 1));
            this.holder.graphics.beginFill(9474192);
            this.holder.graphics.drawRect(0, 0, this.cwidth, _loc_1);
            this.holder.graphics.beginFill(16777215);
            this.holder.graphics.drawRect(1, 1, this.cwidth - 2, _loc_1 - 2);
            this.holder.graphics.endFill();
            this.holder.y = 19;
            this.holder.x = 0;
            this.holder.addChild(this.content);
            if (this.content.height > 180)
            {
                _loc_1 = this.content.height;
                this.sb = new Scrollbar(this.content, new Rectangle(0, 0, this.cwidth, 178));
                this.sb.y = 1;
                this.sb.x = this.cwidth - 17;
                this.sb.SetContentHeight(_loc_1);
                this.holder.addChild(this.sb);
            }
            return;
        }// end function

        private function HideContent() : void
        {
            this.active = false;
            AppState.TextInteraction = false;
            stage.removeEventListener(MouseEvent.CLICK, this.ClickOutside, true);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KBKeyDown, true);
            this.removeChild(this.holder);
            dispatchEvent(new SelectEvent(SelectEvent.CLOSE, ""));
            return;
        }// end function

        private function KBKeyDown(event:KeyboardEvent) : void
        {
            var _loc_2:SelectItem = null;
            event.stopImmediatePropagation();
            if (event.keyCode === 38)
            {
                _loc_2 = this.MarkIndex((this.markedIndex - 1));
                this.sb.ScrollTo(_loc_2.y);
                dispatchEvent(new SelectEvent(SelectEvent.HOVER, _loc_2.name));
            }
            else if (event.keyCode == 40)
            {
                _loc_2 = this.MarkIndex((this.markedIndex + 1));
                this.sb.ScrollTo(_loc_2.y + _loc_2.height);
                dispatchEvent(new SelectEvent(SelectEvent.HOVER, _loc_2.name));
            }
            else if (event.keyCode == 13 && this.active)
            {
                if (this.item != null)
                {
                    this.item.Select(false);
                }
                this.item = this.MarkIndex(this.markedIndex);
                this.HideContent();
                this.tb.text = this.item.text;
                this.markedIndex = this.item.index;
                dispatchEvent(new SelectEvent(SelectEvent.COMMAND, this.item.name));
            }
            return;
        }// end function

        private function Open(event:Event) : void
        {
            this.active = Boolean(!this.active);
            if (this.active)
            {
                stage.focus = this;
                this.addChild(this.holder);
                AppState.TextInteraction = true;
                stage.addEventListener(MouseEvent.CLICK, this.ClickOutside, true);
                stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KBKeyDown, true, 0, true);
            }
            else
            {
                this.HideContent();
            }
            return;
        }// end function

        private function ClickOutside(event:MouseEvent) : void
        {
            if (!this.hitTestPoint(stage.mouseX, stage.mouseY))
            {
                this.HideContent();
            }
            return;
        }// end function

        public function get value() : String
        {
            return this.item != null ? (this.item.name) : ("");
        }// end function

    }
}
